#ifndef DVDATATABLECONTROLLER_H
#define DVDATATABLECONTROLLER_H

#include <QString>
#include <QStringList>
#include <QList>
#include <QMap>
#include <QDebug>
#include "dvDefinitions.h"

typedef QMap<int, DV_REGION> Regions;

typedef QMap<int, double> Data;
typedef QMap<int, int> Refs;
typedef QMap<QString, Data*> dvMapData;
typedef QMap<QString, Refs*> dvMapRefs;
typedef QMap<int, QVector<QPointF> > dvMapPoints;

class dvDataPack
{
public:
    dvDataPack(){}
    ~dvDataPack()
    {
        foreach(QString date, listDate)
        {
            Data *data = mapData.value(date);
            if (data)
            {
                delete data;
            }
        }
        foreach(QString date, listDate)
        {
            Refs *refs = mapRefs.value(date);
            if (refs)
            {
                delete refs;
            }
        }
        listDate.clear();
        mapData.clear();
        mapRefs.clear();
        //qDebug() << "dvDataPack was successfully deleted";
    }

    DV_INDICATOR indicator;
    QStringList  listDate;
    dvMapData    mapData;
    dvMapRefs    mapRefs;
};

typedef QList<dvDataPack*> dvListIndicators;

class dvDataTableController
{
public:
    dvDataTableController();
    ~dvDataTableController();

    void insertIndicator(const DV_INDICATOR& indicator);
    bool removeIndicator(const QVariant& indicatorId);
    void removeAllIndicators();

    int  findIndicator(const QVariant& indicatorId);

    bool insertData(const QVariant& indicatorId, const QString& date,
                    const QVariant& regionId, const QVariant& value);
    bool changeData(const QVariant& indicatorId, const QString& oldDate,
                    const QString& newDate, const QVariant& regionId, const QVariant &value);
    bool changeRegion(const QVariant& indicatorId, const QString& date,
                      const QVariant& oldRegionId, const QVariant& newRegionId,
                      const QVariant &value);
    bool removeData(const QVariant& indicatorId, const QString& date,
                    const QVariant& regionId);

    int indicatorCount();

    bool isExistData(const QVariant& indicatorId, const QString& date,
                     const QVariant& regionId);

    DV_INDICATOR* getIndicatorInfo(int index);

    Data* getIndicatorValuesAtDate(const QVariant& indicatorId, const QString& date);

    QString getStartDateForIndicator(const QVariant& indicatorId);
    QString getEndDateForIndicator(const QVariant& indicatorId);
    int getDateCountForIndicator(const QVariant& indicatorId);
    QString getDateForIndicatorAt(const QVariant& indicatorId, int index);

    double getMaxIndicatorValueAtDate(const QVariant& indicatorId, const QString& date);
    double getMinIndicatorValueAtDate(const QVariant& indicatorId, const QString& date);

    double getPercentValueAtDateForRegion(const QVariant& indicatorId, const QString& date,
                                          const QVariant& regionId);

    QStringList getListOfDatesForIndicator(const QVariant& indicatorId);

    dvMapPoints getAllDataForIndicator(const QVariant& indicatorId);

private:
    dvListIndicators m_listIndicators;
};

#endif // DVDATATABLECONTROLLER_H

























